//package com.fs.book.filter;
//
//import com.alibaba.fastjson.JSON;
//import com.fasterxml.jackson.databind.ObjectMapper;
//import org.springframework.web.filter.OncePerRequestFilter;
//
//import javax.servlet.FilterChain;
//import javax.servlet.ServletException;
//import javax.servlet.annotation.WebFilter;
//import javax.servlet.http.HttpServletRequest;
//import javax.servlet.http.HttpServletResponse;
//import javax.servlet.http.HttpSession;
//import java.io.IOException;
//import java.io.PrintWriter;
//import java.util.HashMap;
//import java.util.Map;
//
///**
// * 检查是否登录
// */
//@WebFilter("/*")
//public class CheckLoginFilter extends OncePerRequestFilter {
//    @Override
//    protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {
//        String token = request.getParameter("token");
//        String url = request.getRequestURI();
//        HttpSession session = request.getSession();
//        if(url.equals("/login.html")|| url.equals("/users/login")
//                || url.startsWith("/common")
//                || url.startsWith("/script")
//                || url.equals("/common/detail.html")
//        || url.startsWith("/style")){
//            //放行
//            filterChain.doFilter(request,response);
//        }else{
//            if(token == null || token.isEmpty() || !token.equals(session.getAttribute("token"))){ //前端没有传递token
//                //不是ajax请求, 如果是ajax请求, 响应的json
////
//                String xhr = request.getHeader("X-Requested-With");
//                if(xhr!=null){//有,ajax请求
//                    // 响应的json
//
//                    //writeValueAsString() 把一个对象转换为json
//                    Map map = new HashMap();
//                    map.put("status",401);
//                    map.put("error","token失效,请重新登录!");
//                    //jackson 操作json
//                    //ObjectMapper objectMapper = new ObjectMapper();
//                    //String json = objectMapper.writeValueAsString(map);
//
//                    //fasetjson 阿里巴巴
//                    String json = JSON.toJSONString(map);
//
//                    //gson 扩展
//
//                    //响应json数据  响应流
//                    response.setContentType("application/json;charset=UTF-8");
//                    PrintWriter writer = response.getWriter();
//                    writer.print(json);
//                    writer.flush();
//
//                }else{
//                    response.sendRedirect("/login.html");
//                }
//                 return;
//            }
//            //放行
//            filterChain.doFilter(request,response);
//        }
//    }
//}
